package 每日一题.array.替换空格;

/**
 * @author 标记
 * @date 2021/1/29
 */
public class Solution {

    /**
     * Q:替换空格
     * 【例子：】
     *      输入："We Are Happy"
     *      输出："We%20Are%20Happy"
     * @param args
     */
    public static void main(String[] args) {
        String str = "We Are Happy";
        String replace1 = str.replace(" ", "%20");
        System.out.println(replace1);
        StringBuffer replace = replace(str);
        System.out.println(replace);
    }

    /**
     * 新的数组存储
     * @param str
     * @return
     */
    public static StringBuffer replace(String str) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < str.length() ; i++) {
            char c = str.charAt(i);
            if (c == ' ') {
                sb.append("%20");
            } else {
                sb.append(c);
            }
        }

        return sb;
    }
}
